import { Card } from "./card.js"
export class Poker {//注意把constructor,output_card里的第二个for循环j=0;j<14;以及24改成52
    cards = new Array(24)
    suits = ['红桃', '黑桃', '方块', '草花']
    constructor() {
        let index = 0
        for (let i = 0; i < this.suits.length; i++) {
            for (let j = 9; j < 15; j++) {
                let card = new Card(this.suits[i], j)//定义扑克牌
                this.cards[index++] = card//把每一张扑克牌存入数组
            }
        }
    }
    output_card() {//让52张牌打在同一行上
        let index = 0
        for (let i = 0; i < this.suits.length; i++) {
            for (let j = 9; j < 15; j++) {
                if (index % 6 === 0)//13张牌一换行
                { console.log(); }
                process.stdout.write(this.cards[index++] + ' ')//把下标带进数组里
                //(card里的tostring方法不可以乱命名，否则process方法无法直接找到)
            }
        }
    }
    shuffle_card()//洗牌方法
    {
        for (let i = 0; i < this.cards.length; i++) {
            let random_index = Math.floor(Math.random() * this.cards.length)//数组下标
            let tmp_card = this.cards[i]
            this.cards[i] = this.cards[random_index]
            this.cards[random_index] = tmp_card
        }
    }
    get_one_hand() {
        this.shuffle_card()//确保调用一手牌的时候洗过牌，类之间的调用用this
        let onehand = []
        for (let i = 0; i < 5; i++) {
            onehand.push(this.cards[i])
        }
        return onehand
    }
    get_special_one_hand() {
        let onehand = []
        let card = new Card('红桃', 1)
        onehand.push(card)
        card = new Card('红桃', 6)
        onehand.push(card)
        card = new Card('黑桃', 5)
        onehand.push(card)
        card = new Card('方片', 8)
        onehand.push(card)
        card = new Card('黑桃', 6)
        onehand.push(card)
        return onehand
    }
    output_onehand(onehand) {
        for (let card of onehand) {
            process.stdout.write(card + ' ')
        }
    }
    get_one_card(index)
    {
        return this.cards[index]
    }
    get_type(onehand) {
        let suit_set = new Set()
        let rank_set = new Set()
        let card_map = new Map()
        for (let card of onehand) {
            suit_set.add(card.suit)
            rank_set.add(card.rank)
            if (card_map.has(card.rank)) {
                let count = card_map.get(card.rank)
                count++
                card_map.set(card.rank, count)
            }
            else {
                card_map.set(card.rank, 1)
            }
        }
        onehand.sort((a, b) => a.rank - b.rank)

        let isSameColor = false
        let isStraight = false
        if (suit_set.size === 1) {//编程时并列的情况可以全用if
            isSameColor = true
            // return '同花'
        }
        if (onehand[4].rank - onehand[0].rank === 4 && rank_set.size === 5) {
            isStraight = true
            // return '顺子'
        }
        if (isSameColor === true && isStraight === true) {
            return '同花顺'
        }
        if (isSameColor === true) {
            return '同花'
        }
        if (isStraight === true) {
            return '顺子'
        }
        if (rank_set.size === 4) {
            return '一对'
        }
        if (rank_set.size === 5) {
            return '无对'
        }
        if (rank_set.size === 2) {
            let arr = [...card_map.values()]
            if (arr.includes(4)) {
                return '四条'
            }
            if (arr.includes(3)) {
                return '满堂红'
            }
        }
        if (rank_set.size === 3) {
            let arr = [...card_map.values()]
            if (arr.includes(3)) {
                return '三条'
            }
            if (arr.includes(2)) {
                return '两对'
            }
        }
    }
}
// let poker = new Poker()
// poker.output_card()
// console.log();
// console.log();
// console.log('洗牌之后：');
// let hand = poker.get_one_hand()
// poker.output_card()
// console.log();
// console.log();
// console.log('分到的一手牌：');
// console.log();
// poker.output_onehand(hand)
// let get_type = poker.get_type(hand)
// console.log();
// console.log(get_type);
